package com.app.GoCuro;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public abstract class BaseActivity extends Activity {

	// handle the opening of the menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.logout_button:
			initiateLogoutDialog();
			break;
		case R.id.about_button:
			initiateAboutDialog();
			break;
		case R.id.settings_button:
			initiateSettingsDialog();
			break;
		}
		return true;

	}

	/*
	 * 
	 * Handles creation of the settings dialog
	 */
	private void initiateSettingsDialog() {
		final Dialog settingsDialog = new Dialog(BaseActivity.this);
		settingsDialog.setContentView(R.layout.settings);
		settingsDialog.setTitle(R.string.settings);

		// setup the pdf type spinner
		Spinner pdfTypeSpinner = (Spinner) settingsDialog
				.findViewById(R.id.barcode_type_spinner);
		ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
				this, R.array.barcode_types,
				android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		pdfTypeSpinner.setAdapter(adapter1);

		// setup the default jobsite spinner
		Spinner jobsiteSpinner = (Spinner) settingsDialog
				.findViewById(R.id.default_jobsite_spinner);

		// TODO fix this once pulling from database is complete
		ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
				this, (Integer) null, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		jobsiteSpinner.setAdapter(adapter2);

		Button dismissButton = (Button) settingsDialog
				.findViewById(R.id.dismiss_settings_button);
		dismissButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				settingsDialog.dismiss();
			}
		});

		settingsDialog.show();

	}

	/*
	 * 
	 * creates and about dialog with information about the app
	 */
	private void initiateAboutDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.about_info)
				.setCancelable(true)
				.setNeutralButton(R.string.dismiss,
						new DialogInterface.OnClickListener() {

							// handle the dismiss button
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();

							}
						});

		// show the about dialog
		AlertDialog aboutDialog = builder.create();
		aboutDialog.show();

	}

	/*
	 * 
	 * handles the creation and actions within the popup window
	 */
	private void initiateLogoutDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.logout_prompt)
				.setCancelable(false)
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {

							// handle confirming logout
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO right now just creates the login
								// interface again, this will need greater logic
								// later
								Intent loginIntent = new Intent(
										BaseActivity.this, LoginActivity.class);
								BaseActivity.this.startActivity(loginIntent);

							}
						})
				.setNegativeButton(R.string.no,
						new DialogInterface.OnClickListener() {

							// handle canceling logout
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();

							}
						});

		// start the alert
		AlertDialog alert = builder.create();
		alert.show();
	}

}
